package com.xingfly.repository;

import com.xingfly.model.Comment;
import com.xingfly.model.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by SuperS on 2016/12/7.
 */
public interface CommentRepository extends JpaRepository<Comment, Long> {
    //根据ObjectType和ObjectId查询Comments 并且通过 Pageable参数对查询结果进行分页
    List<Comment> findByCommentObjectTypeAndCommentObjectId(int objectType, Long objectId, Pageable pageable);

    //使用JPQL语句查询 Comment 作者
    @Query("SELECT c.author FROM Comment c WHERE c.id=:id")
    User findAuthorByCommentId(@Param("id") Long commentId);

    //使用JPQL语句查询指定 Comment 的数量
    @Query("SELECT COUNT(c) FROM Comment c WHERE c.commentObjectType = :type AND c.commentObjectId=:id ")
    long getCommentsCount(@Param("type") int objectType, @Param("id") Long objectId);
}
